﻿namespace PreGUI.Widgets
{
    using PreGUI.Interfaces;
    using UnityEngine;
    using UnityEngine.UI;
    using PreGUI.Helpers;

    /// <summary>
    /// PreGui Panel Control
    /// </summary>
    public class PGPanel : IPGControl
    {
        public int Top, Left, Bottom, Right;
        public IPGControl Child;

        public GameObject LayoutControls(PreGui gui, GameObject parent, string windowName)
        {
            var gameObject = new GameObject("PGPanel");
            gameObject.transform.SetParent(parent.transform, false);

            var image = gameObject.AddComponent<Image>();
            image.color = gui.PanelColor;
            image.sprite = gui.ControlSprite;
            image.type = Image.Type.Sliced;

            var layout = gameObject.AddComponent<HorizontalLayoutGroup>();
            layout.childForceExpandHeight = false;
            layout.padding = new RectOffset(Left, Right, Top, Bottom);
            layout.spacing = 2f;

            if (Child != null)
                Child.LayoutControls(gui, gameObject, windowName);

            return gameObject;
        }


        public IPGControl GenerateFromXmlNode(System.Xml.XmlNode node)
        {
            Top = System.Convert.ToInt32(node.Attributes["Top"].Value);
            Left = System.Convert.ToInt32(node.Attributes["Left"].Value);
            Bottom = System.Convert.ToInt32(node.Attributes["Bottom"].Value);
            Right = System.Convert.ToInt32(node.Attributes["Right"].Value);
            Child = node.HasChildNodes ? node.ChildNodes.Item(0).XmlProcessChildControl() : null;

            return this;
        }
    }
}

